<template>
	<view class="container">
		<!-- 店铺和金额信息 -->
		<view class="shop-info">
			<view class="shop-name-amount">
				<!-- <text class="shop-name">店铺</text> -->
				<text class="amount-info">金额小计 ¥{{totalAmount}}，共 {{totalCount}} 件商品</text>
			</view>
		</view>

		<!-- 分割线 -->
		<view class="divider"></view>

		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods-item" v-for="(goods, index) in goodsList" :key="goods.id">
				<!-- 商品图片和基本信息 -->
				<view class="goods-main">
					<view class="goods-image">
						<image v-if="goods.photo" :src="goods.photo" mode="aspectFill" class="image"></image>
						<view v-else class="image-placeholder">
							<text>暂无图片</text>
						</view>
					</view>
					<view class="goods-info">
						<view class="goods-name">
							<text class="name-text">{{goods.name}}</text>
						</view>
						<view class="goods-attributes" v-if="goods.classifyName">
							<text class="attribute-text">{{goods.classifyName}}</text>
						</view>
						<!-- <view class="goods-tags">
							<text class="tag">支持七天无理由退货</text>
						</view> -->
						<view class="goods-price">
							<text class="price-text">¥{{goods.price}}</text>
							<text class="quantity">x{{goods.buyNum || 1}}</text>
						</view>
					</view>
				</view>

				<!-- 赠品信息 -->
				<view class="gift-info" v-if="goods.gifts && goods.gifts.length > 0">
					<view class="gift-item" v-for="gift in goods.gifts" :key="gift.id">
						<text class="gift-label">赠品</text>
						<text class="gift-name">{{gift.name}}</text>
					</view>
				</view>

				<!-- 分割线（最后一个商品不显示） -->
				<view class="item-divider" v-if="index !== goodsList.length - 1"></view>
			</view>
		</view>

		<!-- 底部提示信息 -->
		<view class="bottom-tips">
			<text class="tips-text">若对价格有所疑问，可点击查看详情</text>
			<u-icon name="thumb-up" color="#1989fa" size="16"></u-icon>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [],
				totalAmount: 0,
				totalCount: 0
			};
		},
		onLoad(options) {
			if (options.data) {
				try {
					const data = JSON.parse(decodeURIComponent(options.data));
					this.goodsList = data;
					this.calculateTotal();
					console.log('商品清单数据:', this.goodsList);
				} catch (e) {
					console.error('解析商品数据失败', e);
					uni.showToast({
						title: '数据加载失败',
						icon: 'none'
					});
				}
			}
		},
		methods: {
			calculateTotal() {
				let amount = 0;
				let count = 0;

				this.goodsList.forEach(item => {
					const itemCount = item.buyNum || 1;
					const itemAmount = item.price * itemCount;
					amount += itemAmount;
					count += itemCount;
				});

				this.totalAmount = amount.toFixed(2);
				this.totalCount = count;
			},

			handleBack() {
				uni.navigateBack();
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background-color: #f6f6f6;
		padding-bottom: 40rpx;
	}

	/* 头部样式 */
	.header {
		height: 88rpx;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
		position: sticky;
		top: 0;
		z-index: 10;
	}

	.header-back {
		width: 60rpx;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-start;
	}

	.header-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}

	.header-placeholder {
		width: 60rpx;
	}

	/* 店铺信息 */
	.shop-info {
		background-color: #ffffff;
		padding: 30rpx;
	}

	.shop-name-amount {
		display: flex;
		flex-direction: column;
	}

	.shop-name {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 10rpx;
	}

	.amount-info {
		font-size: 28rpx;
		color: #666666;
	}

	/* 分割线 */
	.divider {
		height: 2rpx;
		background-color: #f0f0f0;
		margin: 0 30rpx;
	}

	/* 商品列表 */
	.goods-list {
		background-color: #ffffff;
		margin: 20rpx 0;
	}

	.goods-item {
		padding: 30rpx;
	}

	.goods-main {
		display: flex;
	}

	.goods-image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 12rpx;
		overflow: hidden;
		background-color: #f5f5f5;
	}

	.image {
		width: 100%;
		height: 100%;
	}

	.image-placeholder {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #e0e0e0;
	}

	.image-placeholder text {
		font-size: 24rpx;
		color: #999999;
	}

	.goods-info {
		flex: 1;
		margin-left: 24rpx;
		display: flex;
		flex-direction: column;
	}

	.goods-name {
		margin-bottom: 12rpx;
	}

	.name-text {
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.goods-attributes {
		margin-bottom: 12rpx;
	}

	.attribute-text {
		font-size: 26rpx;
		color: #999999;
	}

	.goods-tags {
		margin-bottom: 20rpx;
	}

	.tag {
		font-size: 24rpx;
		color: #1989fa;
		background-color: #ecf5ff;
		padding: 6rpx 12rpx;
		border-radius: 6rpx;
	}

	.goods-price {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.price-text {
		font-size: 32rpx;
		font-weight: bold;
		color: #ff463c;
	}

	.quantity {
		font-size: 28rpx;
		color: #999999;
	}

	/* 赠品信息 */
	.gift-info {
		margin-top: 20rpx;
		padding: 20rpx;
		background-color: #f8f8f8;
		border-radius: 8rpx;
	}

	.gift-item {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.gift-item:last-child {
		margin-bottom: 0;
	}

	.gift-label {
		font-size: 24rpx;
		color: #ff463c;
		background-color: #ffeae9;
		padding: 4rpx 12rpx;
		border-radius: 4rpx;
		margin-right: 12rpx;
	}

	.gift-name {
		font-size: 26rpx;
		color: #666666;
	}

	/* 商品项分割线 */
	.item-divider {
		height: 1rpx;
		background-color: #f0f0f0;
		margin: 30rpx -30rpx 0 -30rpx;
	}

	/* 底部提示 */
	.bottom-tips {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 40rpx 30rpx;
	}

	.tips-text {
		font-size: 26rpx;
		color: #999999;
		margin-right: 10rpx;
	}
</style>